﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;

using System.Web.UI.WebControls;

using Module.Common.Models;
using Module.Core.Common;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;


namespace Module.Core.BusinessLogic.CmsContents
{
    public class CMSZonesBL
    {
        /// <summary>
        /// Saves a record to the CMS_Zones table.
        /// </summary>
        public static int Insert(CMSZonesInfo cMSZonesInfo)
        {
            try
            {
                return new CMSZonesDAO().Insert(cMSZonesInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Saves a record to the CMS_Zones table.
        /// </summary>
        public static void Update(CMSZonesInfo cMSZonesInfo)
        {
            try
            {
                new CMSZonesDAO().Update(cMSZonesInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Delete a record from the CMS_Zones table.
        /// </summary>
        public static void Delete(int zoneID)
        {
            try
            {
                new CMSZonesDAO().Delete(zoneID);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Selects a single record from the CMS_Zones table.
        /// </summary>        
       
        public static CMSZonesInfo Select(int zoneId)
        {
            try
            {
                CMSZonesInfo cmsZonesInfo;            
                cmsZonesInfo = new CMSZonesDAO().Select(zoneId);
                if (cmsZonesInfo != null)               
                    return cmsZonesInfo;
                return null;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// Selects all records from the CMS_Zones table.
        /// </summary>
        public static CHRTList<CMSZonesInfo> SelectAll()
        {
            try
            {
                return new CMSZonesDAO().SelectAll();
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public static CHRTList<CMSZonesInfo> GetParentZones(int parentId, int lang, int roleID, int userID)
        {
            try
            {
                return new CMSZonesDAO().GetParentZones(parentId, lang, roleID, userID);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public static CHRTList<CMSZonesInfo> SelectModule(int? Type, string Para_1, string Para_2)
        {
            try
            {
                return new CMSZonesDAO().SelectModule(Type, Para_1, Para_2);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public static DataSet FillToList(int parentID, int lang, int roleId, int userId)
        {
            try
            {
                DataSet ds_source = new DataSet();
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                DataRow row = null;
                DataColumn colTitle = new DataColumn("Title", typeof(string));
                dt.Columns.Add(colTitle);
                DataColumn colZoneID = new DataColumn("ZoneID", typeof(string));
                dt.Columns.Add(colZoneID);
                DataColumn colZoneIDLang = new DataColumn("ZoneIDLang", typeof(string));
                dt.Columns.Add(colZoneIDLang);

                DataColumn colPriority = new DataColumn("Priority", typeof(string));
                dt.Columns.Add(colPriority);

                DataColumn colModuleMenu = new DataColumn("ModuleMenu", typeof(string));
                dt.Columns.Add(colModuleMenu);

                DataColumn colParentID = new DataColumn("ParentID", typeof(string));
                dt.Columns.Add(colParentID);

                DataColumn colTrangHienThi = new DataColumn("TrangHienThi", typeof(string));
                dt.Columns.Add(colTrangHienThi);

                List<CMSZonesInfo> lstZones = GetParentZones(parentID, lang, roleId, userId);
                foreach (CMSZonesInfo zone in lstZones)
                {
                    row = dt.NewRow();
                    ListItem _item = new ListItem();
                    _item = new ListItem(zone.Title, zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", "0");
                    row["Title"] = _item;
                    dt.Rows.Add(row);
                    //GetSub_Items(_item, ObjConvertUtility.ToInt32(zone.ZoneIDLang), 0, row, dt);
                    GetSub_Items(_item, ObjConvertUtility.ToInt32(zone.ZoneIDLang), 1, roleId, userId, row, dt);
                    row["Priority"] = zone.Priority;
                    row["ZoneID"] = zone.ZoneID;
                    row["ZoneIDLang"] = zone.ZoneIDLang;
                    row["ModuleMenu"] = zone.ModuleMenu;
                    row["ParentID"] = zone.ParentID;
                    row["TrangHienThi"] = zone.TrangHienThi;

                }
                ds_source.Tables.Add(dt);
                return ds_source;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private static void GetSub_Items(ListItem curItem, int ParentID, int lang, int roleId, int userId, DataRow row, DataTable dt)
        {
            try
            {
                List<CMSZonesInfo> lstZones = GetParentZones(ParentID, lang, roleId, userId);
                int _level = ObjConvertUtility.ToInt32(curItem.Attributes["Level"]);
                _level++;
                foreach (CMSZonesInfo zone in lstZones)
                {
                    row = dt.NewRow();
                    ListItem _item = new ListItem();
                    _item = new ListItem(ObjMiscUtility.StringIndent(_level) + zone.Title.ToString(), zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", _level.ToString());
                    dt.Rows.Add(row);
                    GetSub_Items(_item, ObjConvertUtility.ToInt32(zone.ZoneIDLang), 1, roleId, userId, row, dt);
                    row["Title"] = _item;
                    row["Priority"] = zone.Priority;
                    row["ZoneID"] = zone.ZoneID;
                    row["ZoneIDLang"] = zone.ZoneIDLang;
                    row["ModuleMenu"] = zone.ModuleMenu;
                    row["ParentID"] = zone.ParentID;
                    row["TrangHienThi"] = zone.TrangHienThi;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public static void GetSub(ListControl ctrlList, string selectedValue, int lang, int roleId, int userId)
        {
            try
            {
                ctrlList.Items.Clear();
                ctrlList.Items.Insert(0, new ListItem("--------Lựa chọn danh mục --------", "0"));
                CHRTList<CMSZonesInfo> lstZones = GetParentZones(0, lang, roleId, userId);
                foreach (CMSZonesInfo zone in lstZones)
                {
                    ListItem _item = new ListItem(zone.Title, zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", "0");
                    if (_item.Value == selectedValue) _item.Selected = true;
                    ctrlList.Items.Add(_item);
                    GetSubItems(ctrlList, _item, selectedValue, lang, roleId, userId);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private static void GetSubItems(ListControl ctrlList, ListItem curItem, string selectedValue, int lang, int roleId, int userId)
        {

            int zoneId = ObjConvertUtility.ToInt32(curItem.Value);
            int level = ObjConvertUtility.ToInt32(curItem.Attributes["Level"]);
            level++;
            CHRTList<CMSZonesInfo> _lstZones = GetParentZones(zoneId, lang, roleId, userId);
            foreach (CMSZonesInfo _zone in _lstZones)
            {
                ListItem _item = new ListItem(ObjMiscUtility.StringIndent(level) + _zone.Title, _zone.ZoneID.ToString());
                _item.Attributes.Add("Level", level.ToString());
                if (_item.Value == selectedValue) _item.Selected = true;
                ctrlList.Items.Add(_item);
                GetSubItems(ctrlList, _item, selectedValue, lang, roleId, userId);
            }

        }
        public static void GetZoneTypeLatesXML(string fileNameXML, string pathWriteXML, int lang, int checkZone, int parentID, int moduleMenu, string keyOrderby)
        {
            try
            {
                new CMSZonesDAO().GetZoneTypeLatesXML(fileNameXML, pathWriteXML, lang, checkZone, parentID, moduleMenu, keyOrderby);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #region Get dữ liệu Bin vào combobox Lấy theo ModuleType
        private static void GetSubItemsModule(ListControl ctrlList, ListItem curItem, string selectedValue)
        {

            int zoneId = int.Parse(curItem.Value);
            int level = int.Parse(curItem.Attributes["Level"]);
            level++;
            CHRTList<CMSZonesInfo> _lstZones = SelectModule(2, zoneId.ToString(), "");
            if (_lstZones != null && _lstZones.Count > 0)
            {
                foreach (CMSZonesInfo _zone in _lstZones)     
                {
                    ListItem _item = new ListItem(ObjMiscUtility.StringIndent(level) + _zone.Title, _zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", level.ToString());
                    if (_item.Value == selectedValue) _item.Selected = true;
                    ctrlList.Items.Add(_item);
                    GetSubItemsModule(ctrlList, _item, selectedValue);
                }

            }
        }
        public static void GetDataComboType(ListControl ctrlList, string selectedValue, string moduType)
        {
            try
            {
                ctrlList.Items.Clear();
                ctrlList.Items.Insert(0, new ListItem("----------------------", "0"));
                
                CHRTList<CMSZonesInfo> _lstZones = SelectModule(1, "0", moduType);
                if (_lstZones != null && _lstZones.Count > 0)
                {
                    foreach (CMSZonesInfo _zone in _lstZones)            
                    {
                        ListItem _item = new ListItem(_zone.Title, _zone.ZoneID.ToString());
                        _item.Attributes.Add("Level", "0");
                        if (_item.Value == selectedValue) _item.Selected = true;
                        ctrlList.Items.Add(_item);
                        GetSubItemsModule(ctrlList, _item, selectedValue);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public static void LoadDataMenu(string zoneId, Repeater rpt)
        {
            //string UrlFileXml_menu;
            //UrlFileXml_menu = UtilitiesFunctions.GetUrl(Const.VN, Const.folderZone, zoneId);
            //XDocument xDocument = XDocument.Load(UrlFileXml_menu);
            //var feeds = from feed in xDocument.Descendants("Table")
            //            select new
            //            {
            //                zoneId = feed.Element("ZoneID").Value,
            //                tranghienthi = feed.Element("TrangHienThi").Value,
            //                title = feed.Element("Title").Value,
            //                zoneUrl = feed.Element("ZoneUrl").Value
            //            };
            //rpt.DataSource = feeds.Take(10);
            //rpt.DataBind();
        }

        public static int GetZoneId(string zoneUrl)
        {
            try
            {               
                int zoneId = 0;               
                zoneId = new CMSZonesDAO().GetZoneId(zoneUrl);                
                return zoneId;
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        public static string GetArrayZoneId(int zoneId, int lang)
        {
            string strZoneId = "0";
            
            
            CHRTList<CMSZonesInfo> cmsZonesInfo = new CHRTList<CMSZonesInfo>();
            cmsZonesInfo = GetZoneIdFull(zoneId, lang);
            if (cmsZonesInfo !=null && cmsZonesInfo.Count > 0)
            {
                foreach (var zonesInfo in cmsZonesInfo)
                {
                    strZoneId =strZoneId+ "," + zonesInfo.ZoneID ;
                }
            }            
            return "(" + strZoneId + ")";
        }

        public static CHRTList<CMSZonesInfo> GetZoneIdFull(int zoneId, int lang)
        {            
            CHRTList<CMSZonesInfo> cmsZonesInfo = new CHRTList<CMSZonesInfo>();           
            cmsZonesInfo = new CMSZonesDAO().GetZoneIdFull(zoneId, lang);
            if (cmsZonesInfo.Count > 0)
                return cmsZonesInfo;
            else
                return null;
        }

        
        public static string GetZoneUrl(int zoneId, int lang,string linkUrl)
        {
            string zoneUrl=string.Empty;
            int parentId;
            if (linkUrl.IndexOf("http://") > 0 || linkUrl.IndexOf(".") > 0)
            {
                return linkUrl;
            }
            else
            {
                CMSZonesInfo cmsZonesInfo = CMSZonesBL.Select(zoneId);
                if (cmsZonesInfo != null)
                {
                    parentId = cmsZonesInfo.ParentID;
                    if (parentId > 0)
                    {
                        zoneUrl = GetZoneUrl(parentId, lang, linkUrl) + "/" + cmsZonesInfo.KeyZone;
                    }
                    else
                    {
                        zoneUrl = "/" + cmsZonesInfo.KeyZone;
                    }
                }
            }
            
            return zoneUrl;
        }

        public static void UpdateZoneUrl(int zoneId,string zoneUrl)
        {
            try
            {
                new CMSZonesDAO().UpdateZoneUrl(zoneId,zoneUrl);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
              
    }
}